We would like to use visualization to find out some patterns of felony frequency in NYC regarding time and date. We want to know how month, day of the week, and specific hour of the day are associated with the felony counts.
First, we’ll explore trends of felony crimes from 2016 to September of 2022. We want to identify general trends over time and to see if there were any significant changes before and during the covid-19 pandemic.
num_days = function(month, year) {
year = as.integer(year)
months = 1:12
names(months) = month.abb
month = months[month]
as.numeric(strftime(as.Date(paste(year + month %/% 12, month %% 12 + 1, "01", sep = "-")) - 1, "%d"))
}
complaint %>%
filter(level == "FELONY") %>%
mutate(year = fct_rev(year)) %>%
group_by(year, month) %>%
dplyr::summarize(mean_freq = n() / num_days(month, year)) %>%
plot_ly(
x = ~month, y = ~year, z = ~mean_freq,
type = "heatmap"
) %>%
colorbar(x = 1, y = 1) %>%
layout(
title = "Average Daily Felony Frequency by Year and Month",
xaxis = list(title = "Month"),
yaxis = list(title = "Year")
)